package com.clever.mall.service.impl;

import com.clever.mall.commons.dto.*;
import com.clever.mall.commons.exception.DatabaseException;
import com.clever.mall.commons.exception.UserException;
import com.clever.mall.commons.result.ConsignmentResult;
import com.clever.mall.commons.result.TotalConsignmentResult;
import com.clever.mall.entity.Consignment;
import com.clever.mall.entity.Product;
import com.clever.mall.entity.User;
import com.clever.mall.mapper.*;
import com.clever.mall.service.SellService;
import com.clever.mall.utils.StatusCode;
import com.clever.mall.vo.ProductCheckVo;
import com.clever.mall.vo.ProductVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author ChenWang
 * @ClassName SellServiceImpl
 * @date 2020/11/11 10:01
 * @since JDK 1.8
 */
@Service
public class SellServiceImpl implements SellService {
    @Resource
    ConsignmentResultMapper consignmentResultMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    ProductMapper productMapper;
    @Resource
    ConsignmentMapper consignmentMapper;
    @Resource
    ProductVoMapper productVoMapper;
    /**
     * 查询指定用户的寄送的退回信息
     * @param userRequestParam  指定的用户
     * @return                  寄送信息
     */
    @Override
    public TotalConsignmentResult getTotalReturnResult(UserRequestParam userRequestParam) {
        TotalConsignmentResult totalConsignmentResult = new TotalConsignmentResult();
        User user = new User();
        BeanUtils.copyProperties(userRequestParam,user);
        user = userMapper.selectUserByUser(user);
        if(Objects.isNull(user)){
            throw new UserException(StatusCode.USER_NOT_EXIST_ERROR);
        }
        totalConsignmentResult.setUserId(user.getUserId());
        List<ConsignmentResult> consignmentResults = consignmentResultMapper.selectAllConsignmentReturnResultByUser(user);
        if(Objects.isNull(consignmentResults)){
            throw new UserException(StatusCode.CONSIGNMENT_NOT_EXITS);
        }
        int totalNum = 0;
        /*遍历获取所有的退回中的商品的数目*/
        for(ConsignmentResult item : consignmentResults){
            List<ProductCheckVo> productCheckVos = item.getProductCheckVos();
            for (ProductCheckVo productCheckVo: productCheckVos){
               if(productCheckVo.getReturnStatus()==1){
                   totalNum++;
                }
            }
        }
        totalConsignmentResult.setConsignmentResults(consignmentResults);
        totalConsignmentResult.setTotal(totalNum);
        return totalConsignmentResult;
    }

    /**
     * 查询指定用户的寄送信息
     * @param userRequestParam  指定的用户
     * @return                  寄送信息
     */
    @Override
    public TotalConsignmentResult getTotalCheckResult(UserRequestParam userRequestParam) {
        TotalConsignmentResult totalConsignmentResult = new TotalConsignmentResult();
        User user = new User();
        BeanUtils.copyProperties(userRequestParam,user);
        user = userMapper.selectUserByUser(user);
        if(Objects.isNull(user)){
            throw new UserException(StatusCode.USER_NOT_EXIST_ERROR);
        }
        totalConsignmentResult.setUserId(user.getUserId());
        List<ConsignmentResult> consignmentResults = consignmentResultMapper.selectAllConsignmentResultByUser(user);
        if(Objects.isNull(consignmentResults)){
            throw new UserException(StatusCode.CONSIGNMENT_NOT_EXITS);
        }
        int totalNum = 0;
        /*设置每个寄送单的统计数据  审核中 审核通过 未审核*/
        for(ConsignmentResult item : consignmentResults){
            List<ProductCheckVo> productCheckVos = item.getProductCheckVos();
            int underCheck = 0;
            int approve = 0 ;
            int reject = 0;
            for (ProductCheckVo productCheckVo: productCheckVos){
                switch (productCheckVo.getCheckStatus()){
                    case 0 : approve++;
                        break;
                    case 1 : underCheck++;
                        break;
                    case 2 : reject++;
                        break;
                    default: throw new DatabaseException(StatusCode.DATABASE_CHECK_STATUS_ERROR);
                }
            }
            item.setApproveNum(approve);
            item.setUnderCheck(underCheck);
            item.setRejectNum(reject);
            totalNum += underCheck;
        }
        totalConsignmentResult.setConsignmentResults(consignmentResults);
        totalConsignmentResult.setTotal(totalNum);
        return totalConsignmentResult;
    }

    /**
     * 查询指定寄售单的未定价的商品信息
     * @param consignmentRequestParam 指定的寄售单
     * @return                         指定寄售单的未定价的商品信息
     */
    @Override
    public List<ProductCheckVo> getProductCheckVos(ConsignmentRequestParam consignmentRequestParam) {
        int id = consignmentRequestParam.getConsignmentId();
        /*
         * 获取指定的寄卖单信息
         */
        Consignment consignment = consignmentMapper.selectConsignmentById(id);
        if(Objects.isNull(consignment)){
            throw new UserException(StatusCode.CONSIGNMENT_NOT_EXITS);
        }
        if(1==consignment.getIsDel()){
            throw new UserException(StatusCode.CONSIGNMENT_HASE_BEEN_DELETE);
        }
        /*
         *  获取寄卖单的未定价的商品信息
         */
        List<ProductCheckVo> productCheckVos = consignmentResultMapper.selectProductCheckVosByIdAndStatus(id, 0);
        if(Objects.isNull(productCheckVos)){
            throw new UserException(StatusCode.USER_CONSIGMENT_NOT_EXIST_ERROR);
        }
        return productCheckVos;
    }

    /**
     * 根据指定的信息更新一系列商品的价格信息
     * @param updateAllPriceRequestParam    传入的指定商品的价格信息
     * @return                              0表示失败,大于0表示成功
     */
    @Override
    public int updateAllPrice(UpdateAllPriceRequestParam updateAllPriceRequestParam) {
        int count = 0;
        if(Objects.isNull(updateAllPriceRequestParam)){
            throw new UserException(StatusCode.DATA_FORMAT_ERROR);
        }
        if(Objects.isNull(updateAllPriceRequestParam.getUpdatePriceRequestParams())){
            throw new UserException(StatusCode.DATA_FORMAT_ERROR);
        }
        Product product ;
        for(UpdatePriceRequestParam item:updateAllPriceRequestParam.getUpdatePriceRequestParams()){
            product = new Product();
            BeanUtils.copyProperties(item,product);
            /*判定产品库存是否存在*/
            if(Objects.isNull(productMapper.selectTheProductById(product.getProductId()))){
                throw new UserException(StatusCode.PRODUCT_NOT_EXIST_ERROR);
            }
            productMapper.updatePriceByProduct(product);
            count++;
        }
        return count;
    }

    /**
     * 根据寄售ID返回对应的寄送单的未通过商品审核信息
     * @param consignmentRequestParam   指定的寄送单
     * @return                          寄送单的审核信息
     */
    @Override
    public ConsignmentResult getConsignmentUnPassResult(ConsignmentRequestParam consignmentRequestParam) {
        return consignmentResultMapper.selectConsignmentUnPassResultById(consignmentRequestParam.getConsignmentId());
    }

    /**
     * 更改指定寄售单下的未审核通过的商品为退回中状态
     * @param consignmentRequestParam   指定的寄售订单信息
     * @return                          影响的行数
     */
    @Override
    public int identifyReturn(ConsignmentRequestParam consignmentRequestParam) {
        if (Objects.isNull(consignmentResultMapper.selectConsignmentResultById(consignmentRequestParam.getConsignmentId()))) {
            throw new UserException(StatusCode.CONSIGNMENT_NOT_EXITS);
        }
        return consignmentResultMapper.updateAllProductCheckByConsignmentIdAndStatus(consignmentRequestParam.getConsignmentId(),1);
    }
    /**
     * 更改指定寄售单下的未审核通过的商品为不要了状态
     * @param consignmentRequestParam   指定的寄售订单信息
     * @return                          影响的行数
     */
    @Override
    public int identifyAbandon(ConsignmentRequestParam consignmentRequestParam) {
        if (Objects.isNull(consignmentResultMapper.selectConsignmentResultById(consignmentRequestParam.getConsignmentId()))) {
            throw new UserException(StatusCode.CONSIGNMENT_NOT_EXITS);
        }
        return consignmentResultMapper.updateAllProductCheckByConsignmentIdAndStatus(consignmentRequestParam.getConsignmentId(),3);
    }

    /**
     * 记录寄卖地址信息
     * @param consignmentBaseInfoRequestParam   指定的寄卖地址信息
     * @return                                  影响的行数
     */
    @Override
    public int saveConsignment(ConsignmentBaseInfoRequestParam consignmentBaseInfoRequestParam) throws ParseException {
        if(Objects.isNull(consignmentBaseInfoRequestParam)){
            throw new UserException(StatusCode.DATA_FORMAT_ERROR);
        }
        Consignment consignment = new Consignment();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(consignmentBaseInfoRequestParam.getPickUpDate());
        BeanUtils.copyProperties(consignmentBaseInfoRequestParam,consignment);
        consignment.setPickUpDate(date);
        return consignmentMapper.insert(consignment);
    }

    /**
     * 获取在售中的所有商品
     * @param userRequestParam 指定的个人信息
     * @param page              页码
     * @param size              大小
     * @return  在售中的所有商品
     */
    @Override
    public List<ProductVo> getOnSaleProductVos(UserRequestParam userRequestParam, int page, int size) {
        User user = new User();
        user.setUserId(userRequestParam.getUserId());
        if(Objects.isNull(userMapper.selectUserByUser(user))){
            throw new UserException(StatusCode.USER_NOT_EXIST_ERROR);
        }
        return productVoMapper.selecOnSaletByUser(user, page, size);
    }
    /**
     * 获取已售出的所有商品
     * @param userRequestParam 指定的个人信息
     * @param page              页码
     * @param size              大小
     * @return  已售出的所有商品
     */
    @Override
    public List<ProductVo> getBeenSoldProductVos(UserRequestParam userRequestParam, int page, int size) {
        User user = new User();
        user.setUserId(userRequestParam.getUserId());
        if(Objects.isNull(userMapper.selectUserByUser(user))){
            throw new UserException(StatusCode.USER_NOT_EXIST_ERROR);
        }
        return productVoMapper.selectBeenSoldByUser(user, page, size);
    }

}
